
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>javase18 IO流原理及字符流输入输出 · JavaSe 基础</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="<yangxin19970404@163.com>">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="javase19 字节输入输出流.html" />
    
    
    <link rel="prev" href="javase17 File类.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="javase01 HashMap基础.html">
            
                <a href="javase01 HashMap基础.html">
            
                    
                    javase01 HashMap基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="javase02 HashMap构造方法分析.html">
            
                <a href="javase02 HashMap构造方法分析.html">
            
                    
                    javase02 HashMap构造方法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="javase03 HashMap总结.html">
            
                <a href="javase03 HashMap总结.html">
            
                    
                    javase03 HashMap总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="javase04 多线程开始.html">
            
                <a href="javase04 多线程开始.html">
            
                    
                    javase04 多线程开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="javase05 静态代理和Lambda表达式.html">
            
                <a href="javase05 静态代理和Lambda表达式.html">
            
                    
                    javase05 静态代理和Lambda表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="javase06 解决个编译错误.html">
            
                <a href="javase06 解决个编译错误.html">
            
                    
                    javase06 解决个编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="javase07 线程停止.html">
            
                <a href="javase07 线程停止.html">
            
                    
                    javase07 线程停止
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="javase08 线程休眠.html">
            
                <a href="javase08 线程休眠.html">
            
                    
                    javase08 线程休眠
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="javase09 线程礼让与强制执行.html">
            
                <a href="javase09 线程礼让与强制执行.html">
            
                    
                    javase09 线程礼让与强制执行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="javase10 线程优先级.html">
            
                <a href="javase10 线程优先级.html">
            
                    
                    javase10 线程优先级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="javase11 守护线程.html">
            
                <a href="javase11 守护线程.html">
            
                    
                    javase11 守护线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="javase12 线程同步.html">
            
                <a href="javase12 线程同步.html">
            
                    
                    javase12 线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="javase13 死锁.html">
            
                <a href="javase13 死锁.html">
            
                    
                    javase13 死锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="javase14 Lock锁.html">
            
                <a href="javase14 Lock锁.html">
            
                    
                    javase14 Lock锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="javase15 解决线程通信的问题.html">
            
                <a href="javase15 解决线程通信的问题.html">
            
                    
                    javase15 解决线程通信的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="javase16 线程池.html">
            
                <a href="javase16 线程池.html">
            
                    
                    javase16 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="javase17 File类.html">
            
                <a href="javase17 File类.html">
            
                    
                    javase17 File类
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.19" data-path="javase18 IO流原理及字符流输入输出.html">
            
                <a href="javase18 IO流原理及字符流输入输出.html">
            
                    
                    javase18 IO流原理及字符流输入输出
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="javase19 字节输入输出流.html">
            
                <a href="javase19 字节输入输出流.html">
            
                    
                    javase19 字节输入输出流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="javase20 缓冲流.html">
            
                <a href="javase20 缓冲流.html">
            
                    
                    javase20 缓冲流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="javase21 转换流.html">
            
                <a href="javase21 转换流.html">
            
                    
                    javase21 转换流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="javase22 打印流.html">
            
                <a href="javase22 打印流.html">
            
                    
                    javase22 打印流
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >javase18 IO流原理及字符流输入输出</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="io&#x6D41;">IO&#x6D41;</h3>
<blockquote>
<p>Java IO&#x539F;&#x7406;</p>
</blockquote>
<ul>
<li>I/O&#x662F;Input/Output&#x7684;&#x7F29;&#x5199;,I/O&#x6280;&#x672F;&#x662F;&#x975E;&#x5E38;&#x5B9E;&#x7528;&#x7684;&#x6280;&#x672F;,&#x7528;&#x4E8E;&#x5904;&#x7406;&#x8BBE;&#x5907;&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x636E;&#x4F20;&#x8F93;.&#x5982;&#x8BFB;&#x5199;&#x6587;&#x4EF6;,&#x7F51;&#x7EDC;&#x901A;&#x8BAF;</li>
<li>Java&#x7A0B;&#x5E8F;&#x4E2D;,&#x5BF9;&#x4E8E;&#x6570;&#x636E;&#x7684;&#x8F93;&#x5165;/&#x8F93;&#x51FA;&#x64CD;&#x4F5C;&#x4EE5; <strong>&quot;&#x6D41;&quot;</strong> &#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;</li>
<li>java.io&#x5305;&#x4E0B;&#x63D0;&#x4F9B;&#x4E86;&#x5404;&#x79CD;&#x6D41;&#x7684;&#x63A5;&#x53E3;,&#x7528;&#x4EE5;&#x83B7;&#x53D6;&#x4E0D;&#x540C;&#x79CD;&#x7C7B;&#x7684;&#x6570;&#x636E;,&#x5E76;&#x901A;&#x8FC7;&#x6807;&#x51C6;&#x7684;&#x65B9;&#x6CD5;&#x8F93;&#x5165;&#x6216;&#x8F93;&#x51FA;&#x6570;&#x636E;</li>
</ul>
<blockquote>
<p>&#x6D41;&#x7684;&#x5206;&#x7C7B;</p>
</blockquote>
<ul>
<li>&#x6309;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5355;&#x4F4D;&#x4E0D;&#x540C;&#x5206;&#x4E3A;: <strong>&#x5B57;&#x8282;&#x6D41;(8 bit),&#x5B57;&#x7B26;&#x6D41;(16 bit)</strong></li>
<li>&#x6309;&#x6570;&#x636E;&#x6D41;&#x7684;&#x6D41;&#x5411;&#x4E0D;&#x540C;&#x5206;&#x4E3A;:&#x8F93;&#x5165;&#x6D41;,&#x8F93;&#x51FA;&#x6D41;</li>
<li>&#x6309;&#x6D41;&#x7684;&#x89D2;&#x8272;&#x4E0D;&#x540C;&#x5206;&#x4E3A;:&#x8282;&#x70B9;&#x6D41; ,&#x5904;&#x7406;&#x6D41;</li>
</ul>
<p><img src="./imgs/&#x5FAE;&#x4FE1;&#x622A;&#x56FE;_20210317105207.png" alt=""></p>
<blockquote>
<p>IO&#x6D41;&#x4F53;&#x7CFB;</p>
</blockquote>
<p><img src="./imgs/&#x5FAE;&#x4FE1;&#x622A;&#x56FE;_20210317105323.png" alt="IO&#x6D41;&#x4F53;&#x7CFB;"></p>
<p><img src="./imgs/&#x5FAE;&#x4FE1;&#x622A;&#x56FE;_20210317110044.png" alt=""></p>
<blockquote>
<p>&#x6570;&#x636E;&#x8BFB;&#x5165;</p>
</blockquote>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">test</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException </span>{
        FileReader fileReader = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//&#x5B9E;&#x4F8B;&#x5316;file&#x5BF9;&#x8C61;,&#x6307;&#x660E;&#x8981;&#x64CD;&#x4F5C;&#x6587;&#x4EF6;</span>
            File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&quot;src\\main\\resources\\imgs\\hello.txt&quot;</span>);
            <span class="hljs-comment">//&#x63D0;&#x4F9B;&#x5177;&#x4F53;&#x7684;&#x6D41;</span>
            fileReader = <span class="hljs-keyword">new</span> FileReader(file);
            <span class="hljs-comment">//&#x6570;&#x636E;&#x7684;&#x8BFB;&#x5165;</span>
            <span class="hljs-comment">//read():&#x8FD4;&#x56DE;&#x8BFB;&#x5165;&#x7684;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;,&#x5982;&#x679C;&#x8FBE;&#x5230;&#x6587;&#x4EF6;&#x672B;&#x5C3E;,&#x8FD4;&#x56DE;-1</span>
            <span class="hljs-keyword">int</span> data ;
            <span class="hljs-keyword">while</span> ((data = fileReader.read()) != -<span class="hljs-number">1</span>) {
                System.out.print((<span class="hljs-keyword">char</span>) data);
            }
        } <span class="hljs-keyword">catch</span> (IOException e) {
            e.printStackTrace();
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">try</span> {
                <span class="hljs-comment">//&#x6D41;&#x7684;&#x5173;&#x95ED;&#x64CD;&#x4F5C;</span>
                <span class="hljs-keyword">if</span> (fileReader != <span class="hljs-keyword">null</span>)
                fileReader.close();
            }<span class="hljs-keyword">catch</span> (IOException e){
                e.printStackTrace();
            }
        }
    }
</code></pre>
<p><strong>&#x4F7F;&#x7528;finally &#x4FDD;&#x8BC1;&#x4E00;&#x5B9A;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x5173;&#x95ED;&#x64CD;&#x4F5C;</strong></p>
<blockquote>
<p>&#x6570;&#x636E;&#x5199;&#x51FA;</p>
</blockquote>
<p><strong>&#x8F93;&#x51FA;&#x64CD;&#x4F5C;,&#x5BF9;&#x5E94;&#x7684;File&#x53EF;&#x4EE5;&#x4E0D;&#x5B58;&#x5728;,&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;,&#x81EA;&#x52A8;&#x521B;&#x5EFA;,&#x5982;&#x679C;&#x5B58;&#x5728;,&#x5219;&#x8986;&#x76D6;</strong></p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">test2</span><span class="hljs-params">()</span> </span>{
        FileWriter fileWriter = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//&#x5B9E;&#x4F8B;&#x5316;file&#x5BF9;&#x8C61;,&#x6307;&#x660E;&#x8981;&#x64CD;&#x4F5C;&#x6587;&#x4EF6;</span>
            File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&quot;src\\main\\resources\\imgs\\hello1.txt&quot;</span>);
            <span class="hljs-comment">//&#x63D0;&#x4F9B;&#x5177;&#x4F53;&#x7684;&#x5199;&#x51FA;&#x6D41;</span>
            <span class="hljs-comment">//append&#x5982;&#x679C;&#x4E3A;true,&#x4E0D;&#x4F1A;&#x5BF9;&#x6E90;&#x6587;&#x4EF6;&#x8986;&#x76D6;,&#x5728;&#x540E;&#x9762;&#x8FFD;&#x52A0;,false&#x7684;&#x8BDD;&#x8986;&#x76D6;,&#x9ED8;&#x8BA4;false</span>
            fileWriter = <span class="hljs-keyword">new</span> FileWriter(file,<span class="hljs-keyword">true</span>);
            <span class="hljs-comment">//&#x6570;&#x636E;&#x7684;&#x5199;&#x51FA;</span>
            fileWriter.write(<span class="hljs-string">&quot;&#x6768;&#x946B;\n&quot;</span>);
            fileWriter.write(<span class="hljs-string">&quot;I have a dream&quot;</span>);
            fileWriter.write(<span class="hljs-string">&quot;Audi RS7&quot;</span>);

        } <span class="hljs-keyword">catch</span> (IOException e) {
            e.printStackTrace();
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">try</span> {
                <span class="hljs-comment">//&#x6D41;&#x7684;&#x5173;&#x95ED;&#x64CD;&#x4F5C;</span>
                <span class="hljs-keyword">if</span> (fileWriter != <span class="hljs-keyword">null</span>)
                fileWriter.close();
            }<span class="hljs-keyword">catch</span> (IOException e){
                e.printStackTrace();
            }
        }
    }
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="javase17 File类.html" class="navigation navigation-prev " aria-label="Previous page: javase17 File类">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="javase19 字节输入输出流.html" class="navigation navigation-next " aria-label="Next page: javase19 字节输入输出流">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"javase18 IO流原理及字符流输入输出","level":"1.19","depth":1,"next":{"title":"javase19 字节输入输出流","level":"1.20","depth":1,"path":"javase19 字节输入输出流.md","ref":"javase19 字节输入输出流.md","articles":[]},"previous":{"title":"javase17 File类","level":"1.18","depth":1,"path":"javase17 File类.md","ref":"javase17 File类.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"<yangxin19970404@163.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JavaSe 基础","language":"zh-hans","gitbook":"3.2.3","description":"JavaSe 基础"},"file":{"path":"javase18 IO流原理及字符流输入输出.md","mtime":"2021-03-17T09:30:54.588Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-17T08:15:36.913Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

